package Servlet;

import java.io.IOException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Dao.*;
import Entity.*;

public class AddRefundProveServlet extends HttpServlet {

    private UserListDao userdao = new UserListDao();
    private AddRefundProveDao addrefunddao = new AddRefundProveDao();
    private BillListDao billdao = new BillListDao();
    private ProveNumDao numdao = new ProveNumDao();

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        String method = request.getParameter("method");
        String path = "pages/Refund_Prove.jsp";
        boolean ac = false;
        if (method.equals("list")) {
            //获得所有未冻结的账户
            List<UserListEntity> list_user = userdao.select_showd();
            request.setAttribute("list_user", list_user);
            //获得所有票据名称
            List<BillEntity> list_bill = billdao.select();
            request.setAttribute("list_bill", list_bill);
            //获得当前的凭证编号
            request.setAttribute("refund_num", numdao.select().get(0).getRefund_num());
        } else if (method.equals("add")) {
            String test_time = request.getParameter("pay_form_time");
            //Time
            java.util.Date date = new java.util.Date();
            Timestamp time = new Timestamp(date.getTime());
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String timer = df.format(time);
            int num = numdao.select().get(0).getRefund_num();
            numdao.update_refund(num + 1);
            String operator = request.getParameter("form_operator");

            RefundProveEntity refund = new RefundProveEntity();
            refund.setNum(num);
            refund.setName(request.getParameter("refund_form_name"));
            refund.setCode(Integer.parseInt(request.getParameter("refund_form_code")));
            //系统 当前 时间
            //refund.setTime(timer);
            //JSP 当前时间
            refund.setTime(test_time);
            refund.setOrder_in(request.getParameter("refund_form_order_in"));
            refund.setRefund_total(Double.parseDouble(request.getParameter("refund_form_refund_total")));
            refund.setSurplus(Double.parseDouble(request.getParameter("refund_form_surplus")));
            refund.setOperator(operator);
            refund.setProject(2);
            addrefunddao.add(refund);
            //更改用户的账户余额
            userdao.reduce_user_surplus(Double.parseDouble(request.getParameter("refund_form_surplus")), Integer.parseInt(request.getParameter("refund_form_code")));
            path = "Add_Refund_Prove_Servlet?method=list";
            ac = true;
        }
        if (ac) {
            response.sendRedirect(path);
        } else {
            request.getRequestDispatcher(path).forward(request, response);
        }
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }
}
